//
// Created by Hoyin on 2024/4/13.
//

#include "DatasetLoader.h"
using namespace cv;


void ImageLoader::_handle() {
	while (_inCount < _dataSize) {
		if (_halt) break;
		if (_dataQueue.size() < _maxQueueSize) {
			Mat img = imread(_files[_inCount]);
			_dataQueue.push(img);
			_inCount++;
		} else {
			std::this_thread::sleep_for(std::chrono::milliseconds(100));
		}
	}
}

void ImageLoader::load() {
	_halt = false;
	std::thread thQueueIn(&ImageLoader::_handle, this);
	thQueueIn.detach();
	Clock cl;
	namespace cno = std::chrono;
	cl.setTick(cno::seconds(1));
	while (_dataQueue.size() < _maxQueueSize) {
		std::cerr << "Loading images... " << cl.duration<cno::seconds>().count() << "\n";
		std::this_thread::sleep_for(cno::milliseconds(1000));
	}
}

cv::Mat ImageLoader::pop() {
	if (_dataQueue.empty()) {
		Mat img;
		return img;
	}
	Mat img = _dataQueue.front();
	_dataQueue.pop();
	_outCount++;
	return img;
}
